using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class CountriesRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class CountriesDbo : DatabaseObject
        {
            public CountriesDbo()
            {
                Name = "Countries";
                GeneratedClassName = "Countries";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "smallint", IsIdentity = true, IsNullable = false, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Code", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "Code" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Name", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "Name" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CurrencyName", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "CurrencyName" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CurrencyISOCode", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 10, Precision = 0, Scale = 0, GeneratedPropertyName = "CurrencyISOCode" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CurrencySymbol", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 10, Precision = 0, Scale = 0, GeneratedPropertyName = "CurrencySymbol" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Convention", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "Convention" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "TaxRate", ColumnType = "smallmoney", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "TaxRate" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "IsVATIncluded", ColumnType = "bit", IsIdentity = false, IsNullable = false, Length = 1, Precision = 1, Scale = 0, GeneratedPropertyName = "IsVATIncluded" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "IsVATGeneral", ColumnType = "bit", IsIdentity = false, IsNullable = false, Length = 1, Precision = 1, Scale = 0, GeneratedPropertyName = "IsVATGeneral" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ArticlesImporter", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "ArticlesImporter" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoresImporter", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "StoresImporter" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SalesImporter", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "SalesImporter" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "InventoryImporter", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "InventoryImporter" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "PIDifferencesImporter", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "PIDifferencesImporter" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SpacesImporter", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "SpacesImporter" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "IsArticleMasterSeparateFile", ColumnType = "bit", IsIdentity = false, IsNullable = false, Length = 1, Precision = 1, Scale = 0, GeneratedPropertyName = "IsArticleMasterSeparateFile" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "IsStoreMasterSeparateFile", ColumnType = "bit", IsIdentity = false, IsNullable = false, Length = 1, Precision = 1, Scale = 0, GeneratedPropertyName = "IsStoreMasterSeparateFile" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "DailySales", ColumnType = "bit", IsIdentity = false, IsNullable = false, Length = 1, Precision = 1, Scale = 0, GeneratedPropertyName = "DailySales" });
              
            }
        }
		
		public static List<CountryRow> Select(Expression<Func<CountryRow, bool>> expression)
        {
            List<CountryRow> rows = new List<CountryRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<CountryRow>(new CountriesDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<CountryRow> Select()
		{
			List<CountryRow> rows = new List<CountryRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<CountryRow>(new CountriesDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<CountryRow> PagedSelect(Expression<Func<CountryRow, bool>> filterExpression, Expression<Func<CountryRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<CountryRow> rows = new List<CountryRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<CountryRow>(new CountriesDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<CountryRow> CountryList)
        {
            CountryRow row;

            while (reader.Read())
            {
                row = new CountryRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					Code = Util.CastDbValue<string>(reader["Code"]),
					Name = Util.CastDbValue<string>(reader["Name"]),
					CurrencyName = Util.CastDbValue<string>(reader["CurrencyName"]),
					CurrencyISOCode = Util.CastDbValue<string>(reader["CurrencyISOCode"]),
					CurrencySymbol = Util.CastDbValue<string>(reader["CurrencySymbol"]),
					Convention = Util.CastDbValue<string>(reader["Convention"]),
					TaxRate = Util.CastDbValue<decimal?>(reader["TaxRate"]),
					IsVATIncluded = Util.CastDbValue<bool>(reader["IsVATIncluded"]),
					IsVATGeneral = Util.CastDbValue<bool>(reader["IsVATGeneral"]),
					ArticlesImporter = Util.CastDbValue<string>(reader["ArticlesImporter"]),
					StoresImporter = Util.CastDbValue<string>(reader["StoresImporter"]),
					SalesImporter = Util.CastDbValue<string>(reader["SalesImporter"]),
					InventoryImporter = Util.CastDbValue<string>(reader["InventoryImporter"]),
					PIDifferencesImporter = Util.CastDbValue<string>(reader["PIDifferencesImporter"]),
					SpacesImporter = Util.CastDbValue<string>(reader["SpacesImporter"]),
					IsArticleMasterSeparateFile = Util.CastDbValue<bool>(reader["IsArticleMasterSeparateFile"]),
					IsStoreMasterSeparateFile = Util.CastDbValue<bool>(reader["IsStoreMasterSeparateFile"]),
					DailySales = Util.CastDbValue<bool>(reader["DailySales"])
                };
                                
                CountryList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(CountryRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<CountryRow>(new CountriesDbo(), row);
            }
        }

		public static void Update(CountryRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<CountryRow>(new CountriesDbo(), row);
            }
        }

		public static void Delete(Expression<Func<CountryRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<CountryRow>(new CountriesDbo(), expression);
            }
        }

	}
}
	
